import 'dotenv/config';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Logger } from '@nestjs/common';
import { TransformInterceptor } from './interceptors/transform/transform.interceptor';
import { HttpExceptionFilter } from './filters/http-exception.filter';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

const PORT = process.env.PORT || 8080;
const PREFIX = process.env.PREFIX || '';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  // 设置url前缀
  app.setGlobalPrefix(PREFIX);

  // 配置api文档信息
  const options = new DocumentBuilder()
    .setTitle('nest-vue-rbac  api文档')
    .setDescription('nest-vue-rbac  api接口文档')
    .setBasePath(PREFIX) // 设置基础的路径
    .addBearerAuth({ type: 'apiKey', in: 'header', name: 'token' }) // 设置请求头的token字段
    .setVersion('0.0.1')
    .build();

  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup(`${PREFIX}/docs`, app, document);

  // 全局拦截器
  app.useGlobalInterceptors(new TransformInterceptor());
  // 全局过滤器
  app.useGlobalFilters(new HttpExceptionFilter());
  await app.listen(PORT);
  Logger.log(`服务已经启动:localhost:${PORT}/${PREFIX}`)
}
bootstrap();
